<?php
namespace app\shop\service\company;
use app\shop\model\company\WorkReportTpl;
use app\common\service\Result;
class WorkReport
{
    public function tplList($data)
    {
        $app_id = request()->app_id;
        $map = [
            ['app_id', '=', $app_id],
        ];
        $title = $data['title'] ?? "";
        if ($title) {
            $map[] = ['title', 'like', "%$title%"];
        }
        $department_id = $data['department_id'] ?? 0;
        if ($department_id > 0) {
            $map[] = ['department_id', '=', $department_id];
        }
        $station_id = $data['station_id'] ?? 0;
        if ($station_id > 0) {
            $map[] = ['station_id', '=', $station_id];
        }
        $type = $data['type'] ?? 0;
        if ($type > 0) {
            $map[] = ['type', '=', $type];
        }
        $page_size = $data['page_size'] ?? 20;
        $lists = WorkReportTpl::where($map)->order('id desc')->with(['department', 'station', 'file'])->append(['type_name'])->paginate($page_size);
        return Result::success($lists);
    }

    public function addTpl($data)
    {
        $data['app_id'] = request()->app_id;
        //$data['handle_user'] = request()->user_id;
        unset($data['id']);
        WorkReportTpl::create($data);
        return Result::success();
    }

    public function editTpl($data)
    {
        try {
            $info = WorkReportTpl::find($data['id']);
            if (!$info) {
                return Result::error('数据不存在');
            }
            //$data['handle_user'] = request()->user_id;
            $info->save($data);
            return Result::success();
        } catch (\Exception $e) {
            return Result::error($e->getMessage());
        }
    }

    public function deleteTpl($id)
    {
        $info = WorkReportTpl::find($id);
        if (!$info) {
            return Result::error('数据不存在');
        }
        $info->delete();
        return Result::success();
    }

    public function tplForm($id)
    {
        $info = WorkReportTpl::with(['file'])->find($id);
        return Result::success($info);
    }
}